package com.atguigu4.util;



import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    /**
     *
     * @return
     * @throws Exception
     * 获取数据库连接操作
     */
    public static Connection getConnection() throws Exception {
        //1.读取配置文件
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

        Properties pros = new Properties();

        pros.load(is);

        //2.获取信息
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

        //3.加载驱动
        Class.forName(driverClass);

        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
    }


    /**
     *
     * @param conn
     * @param ps
     * 关闭资源
     */
    public static void closeResource(Connection conn, PreparedStatement ps){
        try {
            if(conn != null)
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            if(ps != null)
            ps.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void closeResource(Connection conn, PreparedStatement ps, ResultSet rs){
        try {
            if(conn != null)
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            if(ps != null)
            ps.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            if(rs != null)
            rs.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *使用DBCP数据库连接池技术获取数据库连接
     *
     */
    private static DataSource source;
    static{
        try {
            Properties pros = new Properties();
            FileInputStream is = new FileInputStream(new File("src/dbcp.properties"));
            pros.load(is);
            //方式二：
            source = BasicDataSourceFactory.createDataSource(pros);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection2() throws Exception {
        Connection conn = source.getConnection();
        return conn;
    }

    /**
     *使用druid数据库连接池技术获取数据库连接
     *
     */
    private  static DataSource source1;
    static {
        try {
            Properties pros = new Properties();
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
            pros.load(is);
            source1 = DruidDataSourceFactory.createDataSource(pros);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection3() throws SQLException {
        Connection conn = source1.getConnection();
        return conn;
    }

    /**
     *
     * 使用DbUtils.jar包中提供的DbUtils工具类来关闭资源
     * @param conn
     * @param ps
     * @param rs
     */
    public static void closeResource1(Connection conn, PreparedStatement ps, ResultSet rs){
//        try {
//            DbUtils.close(conn);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//        try {
//            DbUtils.close(ps);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//        try {
//            DbUtils.close(rs);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }

        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(rs);
    }
}
